eval.texi 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. @chapter Expression Evaluation
  2. @c man begin EXPRESSION EVALUATION
  3. When evaluating an arithemetic 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. @item ld(var)
  47. Allow to load the value of the internal variable with number
  48. @var{var}, which was previosly stored with st(@var{var}, @var{expr}).
  49. The function returns the loaded value.
  50. @item while(cond, expr)
  51. Evaluate expression @var{expr} while the expression @var{cond} is
  52. non-zero, and returns the value of the last @var{expr} evaluation, or
  53. NAN if @var{cond} was always false.
  54. @item ceil(expr)
  55. Round the value of expression @var{expr} upwards to the nearest
  56. integer. For example, "ceil(1.5)" is "2.0".
  57. @item floor(expr)
  58. Round the value of expression @var{expr} downwards to the nearest
  59. integer. For example, "floor(-1.5)" is "-2.0".
  60. @item trunc(expr)
  61. Round the value of expression @var{expr} towards zero to the nearest
  62. integer. For example, "trunc(-1.5)" is "-1.0".
  63. @item sqrt(expr)
  64. Compute the square root of @var{expr}. This is equivalent to
  65. "(@var{expr})^.5".
  66. @item not(expr)
  67. Return 1.0 if @var{expr} is zero, 0.0 otherwise.
  68. @item pow(x, y)
  69. Compute the power of @var{x} elevated @var{y}, it is equivalent to
  70. "(@var{x})^(@var{y})".
  71. @item random(x)
  72. Return a pseudo random value between 0.0 and 1.0. @var{x} is the index of the
  73. internal variable which will be used to save the seed/state.
  74. @item hypot(x, y)
  75. This function is similar to the C function with the same name; it returns
  76. "sqrt(@var{x}*@var{x} + @var{y}*@var{y})", the length of the hypotenuse of a
  77. right triangle with sides of length @var{x} and @var{y}, or the distance of the
  78. point (@var{x}, @var{y}) from the origin.
  79. @item gcd(x, y)
  80. Return the greatest common divisor of @var{x} and @var{y}. If both @var{x} and
  81. @var{y} are 0 or either or both are less than zero then behavior is undefined.
  82. @end table
  83. The following constants are available:
  84. @table @option
  85. @item PI
  86. area of the unit disc, approximatively 3.14
  87. @item E
  88. exp(1) (Euler's number), approximatively 2.718
  89. @item PHI
  90. golden ratio (1+sqrt(5))/2, approximatively 1.618
  91. @end table
  92. Note that:
  93. @code{*} works like AND
  94. @code{+} works like OR
  95. thus
  96. @example
  97. if A then B else C
  98. @end example
  99. is equivalent to
  100. @example
  101. A*B + not(A)*C
  102. @end example
  103. In your C code, you can extend the list of unary and binary functions,
  104. and define recognized constants, so that they are available for your
  105. expressions.
  106. The evaluator also recognizes the International System number
  107. postfixes. If 'i' is appended after the postfix, powers of 2 are used
  108. instead of powers of 10. The 'B' postfix multiplies the value for 8,
  109. and can be appended after another postfix or used alone. This allows
  110. using for example 'KB', 'MiB', 'G' and 'B' as postfix.
  111. Follows the list of available International System postfixes, with
  112. indication of the corresponding powers of 10 and of 2.
  113. @table @option
  114. @item y
  115. -24 / -80
  116. @item z
  117. -21 / -70
  118. @item a
  119. -18 / -60
  120. @item f
  121. -15 / -50
  122. @item p
  123. -12 / -40
  124. @item n
  125. -9 / -30
  126. @item u
  127. -6 / -20
  128. @item m
  129. -3 / -10
  130. @item c
  131. -2
  132. @item d
  133. -1
  134. @item h
  135. 2
  136. @item k
  137. 3 / 10
  138. @item K
  139. 3 / 10
  140. @item M
  141. 6 / 20
  142. @item G
  143. 9 / 30
  144. @item T
  145. 12 / 40
  146. @item P
  147. 15 / 40
  148. @item E
  149. 18 / 50
  150. @item Z
  151. 21 / 60
  152. @item Y
  153. 24 / 70
  154. @end table
  155. @c man end