eval.texi 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. @end table
  84. The following constants are available:
  85. @table @option
  86. @item PI
  87. area of the unit disc, approximately 3.14
  88. @item E
  89. exp(1) (Euler's number), approximately 2.718
  90. @item PHI
  91. golden ratio (1+sqrt(5))/2, approximately 1.618
  92. @end table
  93. Note that:
  94. @code{*} works like AND
  95. @code{+} works like OR
  96. thus
  97. @example
  98. if A then B else C
  99. @end example
  100. is equivalent to
  101. @example
  102. A*B + not(A)*C
  103. @end example
  104. In your C code, you can extend the list of unary and binary functions,
  105. and define recognized constants, so that they are available for your
  106. expressions.
  107. The evaluator also recognizes the International System number
  108. postfixes. If 'i' is appended after the postfix, powers of 2 are used
  109. instead of powers of 10. The 'B' postfix multiplies the value for 8,
  110. and can be appended after another postfix or used alone. This allows
  111. using for example 'KB', 'MiB', 'G' and 'B' as postfix.
  112. Follows the list of available International System postfixes, with
  113. indication of the corresponding powers of 10 and of 2.
  114. @table @option
  115. @item y
  116. -24 / -80
  117. @item z
  118. -21 / -70
  119. @item a
  120. -18 / -60
  121. @item f
  122. -15 / -50
  123. @item p
  124. -12 / -40
  125. @item n
  126. -9 / -30
  127. @item u
  128. -6 / -20
  129. @item m
  130. -3 / -10
  131. @item c
  132. -2
  133. @item d
  134. -1
  135. @item h
  136. 2
  137. @item k
  138. 3 / 10
  139. @item K
  140. 3 / 10
  141. @item M
  142. 6 / 20
  143. @item G
  144. 9 / 30
  145. @item T
  146. 12 / 40
  147. @item P
  148. 15 / 40
  149. @item E
  150. 18 / 50
  151. @item Z
  152. 21 / 60
  153. @item Y
  154. 24 / 70
  155. @end table
  156. @c man end