rational.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * rational numbers
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * @ingroup lavu_math_rational
  24. * Utilties for rational number calculation.
  25. * @author Michael Niedermayer <michaelni@gmx.at>
  26. */
  27. #ifndef AVUTIL_RATIONAL_H
  28. #define AVUTIL_RATIONAL_H
  29. #include <stdint.h>
  30. #include <limits.h>
  31. #include "attributes.h"
  32. /**
  33. * @defgroup lavu_math_rational AVRational
  34. * @ingroup lavu_math
  35. * Rational number calculation.
  36. *
  37. * While rational numbers can be expressed as floating-point numbers, the
  38. * conversion process is a lossy one, so are floating-point operations. On the
  39. * other hand, the nature of FFmpeg demands highly accurate calculation of
  40. * timestamps. This set of rational number utilities serves as a generic
  41. * interface for manipulating rational numbers as pairs of numerators and
  42. * denominators.
  43. *
  44. * Many of the functions that operate on AVRational's have the suffix `_q`, in
  45. * reference to the mathematical symbol "ℚ" (Q) which denotes the set of all
  46. * rational numbers.
  47. *
  48. * @{
  49. */
  50. /**
  51. * Rational number (pair of numerator and denominator).
  52. */
  53. typedef struct AVRational{
  54. int num; ///< Numerator
  55. int den; ///< Denominator
  56. } AVRational;
  57. /**
  58. * Create an AVRational.
  59. *
  60. * Useful for compilers that do not support compound literals.
  61. *
  62. * @note The return value is not reduced.
  63. * @see av_reduce()
  64. */
  65. static inline AVRational av_make_q(int num, int den)
  66. {
  67. AVRational r = { num, den };
  68. return r;
  69. }
  70. /**
  71. * Compare two rationals.
  72. *
  73. * @param a First rational
  74. * @param b Second rational
  75. *
  76. * @return One of the following values:
  77. * - 0 if `a == b`
  78. * - 1 if `a > b`
  79. * - -1 if `a < b`
  80. * - `INT_MIN` if one of the values is of the form `0 / 0`
  81. */
  82. static inline int av_cmp_q(AVRational a, AVRational b){
  83. const int64_t tmp= a.num * (int64_t)b.den - b.num * (int64_t)a.den;
  84. if(tmp) return (int)((tmp ^ a.den ^ b.den)>>63)|1;
  85. else if(b.den && a.den) return 0;
  86. else if(a.num && b.num) return (a.num>>31) - (b.num>>31);
  87. else return INT_MIN;
  88. }
  89. /**
  90. * Convert an AVRational to a `double`.
  91. * @param a AVRational to convert
  92. * @return `a` in floating-point form
  93. * @see av_d2q()
  94. */
  95. static inline double av_q2d(AVRational a){
  96. return a.num / (double) a.den;
  97. }
  98. /**
  99. * Reduce a fraction.
  100. *
  101. * This is useful for framerate calculations.
  102. *
  103. * @param[out] dst_num Destination numerator
  104. * @param[out] dst_den Destination denominator
  105. * @param[in] num Source numerator
  106. * @param[in] den Source denominator
  107. * @param[in] max Maximum allowed values for `dst_num` & `dst_den`
  108. * @return 1 if the operation is exact, 0 otherwise
  109. */
  110. int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max);
  111. /**
  112. * Multiply two rationals.
  113. * @param b First rational
  114. * @param c Second rational
  115. * @return b*c
  116. */
  117. AVRational av_mul_q(AVRational b, AVRational c) av_const;
  118. /**
  119. * Divide one rational by another.
  120. * @param b First rational
  121. * @param c Second rational
  122. * @return b/c
  123. */
  124. AVRational av_div_q(AVRational b, AVRational c) av_const;
  125. /**
  126. * Add two rationals.
  127. * @param b First rational
  128. * @param c Second rational
  129. * @return b+c
  130. */
  131. AVRational av_add_q(AVRational b, AVRational c) av_const;
  132. /**
  133. * Subtract one rational from another.
  134. * @param b First rational
  135. * @param c Second rational
  136. * @return b-c
  137. */
  138. AVRational av_sub_q(AVRational b, AVRational c) av_const;
  139. /**
  140. * Invert a rational.
  141. * @param q value
  142. * @return 1 / q
  143. */
  144. static av_always_inline AVRational av_inv_q(AVRational q)
  145. {
  146. AVRational r = { q.den, q.num };
  147. return r;
  148. }
  149. /**
  150. * Convert a double precision floating point number to a rational.
  151. *
  152. * In case of infinity, the returned value is expressed as `{1, 0}` or
  153. * `{-1, 0}` depending on the sign.
  154. *
  155. * @param d `double` to convert
  156. * @param max Maximum allowed numerator and denominator
  157. * @return `d` in AVRational form
  158. * @see av_q2d()
  159. */
  160. AVRational av_d2q(double d, int max) av_const;
  161. /**
  162. * Find which of the two rationals is closer to another rational.
  163. *
  164. * @param q Rational to be compared against
  165. * @param q1,q2 Rationals to be tested
  166. * @return One of the following values:
  167. * - 1 if `q1` is nearer to `q` than `q2`
  168. * - -1 if `q2` is nearer to `q` than `q1`
  169. * - 0 if they have the same distance
  170. */
  171. int av_nearer_q(AVRational q, AVRational q1, AVRational q2);
  172. /**
  173. * Find the value in a list of rationals nearest a given reference rational.
  174. *
  175. * @param q Reference rational
  176. * @param q_list Array of rationals terminated by `{0, 0}`
  177. * @return Index of the nearest value found in the array
  178. */
  179. int av_find_nearest_q_idx(AVRational q, const AVRational* q_list);
  180. /**
  181. * Convert an AVRational to a IEEE 32-bit `float` expressed in fixed-point
  182. * format.
  183. *
  184. * @param q Rational to be converted
  185. * @return Equivalent floating-point value, expressed as an unsigned 32-bit
  186. * integer.
  187. * @note The returned value is platform-indepedant.
  188. */
  189. uint32_t av_q2intfloat(AVRational q);
  190. /**
  191. * Return the best rational so that a and b are multiple of it.
  192. * If the resulting denominator is larger than max_den, return def.
  193. */
  194. AVRational av_gcd_q(AVRational a, AVRational b, int max_den, AVRational def);
  195. /**
  196. * @}
  197. */
  198. #endif /* AVUTIL_RATIONAL_H */