rational.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. * rational numbers
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "avassert.h"
  27. //#include <math.h>
  28. #include <limits.h>
  29. #include "common.h"
  30. #include "mathematics.h"
  31. #include "rational.h"
  32. int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max){
  33. AVRational a0={0,1}, a1={1,0};
  34. int sign= (num<0) ^ (den<0);
  35. int64_t gcd= av_gcd(FFABS(num), FFABS(den));
  36. if(gcd){
  37. num = FFABS(num)/gcd;
  38. den = FFABS(den)/gcd;
  39. }
  40. if(num<=max && den<=max){
  41. a1= (AVRational){num, den};
  42. den=0;
  43. }
  44. while(den){
  45. uint64_t x = num / den;
  46. int64_t next_den= num - den*x;
  47. int64_t a2n= x*a1.num + a0.num;
  48. int64_t a2d= x*a1.den + a0.den;
  49. if(a2n > max || a2d > max){
  50. if(a1.num) x= (max - a0.num) / a1.num;
  51. if(a1.den) x= FFMIN(x, (max - a0.den) / a1.den);
  52. if (den*(2*x*a1.den + a0.den) > num*a1.den)
  53. a1 = (AVRational){x*a1.num + a0.num, x*a1.den + a0.den};
  54. break;
  55. }
  56. a0= a1;
  57. a1= (AVRational){a2n, a2d};
  58. num= den;
  59. den= next_den;
  60. }
  61. av_assert2(av_gcd(a1.num, a1.den) <= 1U);
  62. *dst_num = sign ? -a1.num : a1.num;
  63. *dst_den = a1.den;
  64. return den==0;
  65. }
  66. AVRational av_mul_q(AVRational b, AVRational c){
  67. av_reduce(&b.num, &b.den, b.num * (int64_t)c.num, b.den * (int64_t)c.den, INT_MAX);
  68. return b;
  69. }
  70. AVRational av_div_q(AVRational b, AVRational c){
  71. return av_mul_q(b, (AVRational){c.den, c.num});
  72. }
  73. AVRational av_add_q(AVRational b, AVRational c){
  74. av_reduce(&b.num, &b.den, b.num * (int64_t)c.den + c.num * (int64_t)b.den, b.den * (int64_t)c.den, INT_MAX);
  75. return b;
  76. }
  77. AVRational av_sub_q(AVRational b, AVRational c){
  78. return av_add_q(b, (AVRational){-c.num, c.den});
  79. }
  80. AVRational av_d2q(double d, int max){
  81. AVRational a;
  82. #define LOG2 0.69314718055994530941723212145817656807550013436025
  83. int exponent;
  84. int64_t den;
  85. if (isnan(d))
  86. return (AVRational){0,0};
  87. if (isinf(d))
  88. return (AVRational){ d<0 ? -1:1, 0 };
  89. exponent = FFMAX( (int)(log(fabs(d) + 1e-20)/LOG2), 0);
  90. den = 1LL << (61 - exponent);
  91. av_reduce(&a.num, &a.den, (int64_t)(d * den + 0.5), den, max);
  92. return a;
  93. }
  94. int av_nearer_q(AVRational q, AVRational q1, AVRational q2)
  95. {
  96. /* n/d is q, a/b is the median between q1 and q2 */
  97. int64_t a = q1.num * (int64_t)q2.den + q2.num * (int64_t)q1.den;
  98. int64_t b = 2 * (int64_t)q1.den * q2.den;
  99. /* rnd_up(a*d/b) > n => a*d/b > n */
  100. int64_t x_up = av_rescale_rnd(a, q.den, b, AV_ROUND_UP);
  101. /* rnd_down(a*d/b) < n => a*d/b < n */
  102. int64_t x_down = av_rescale_rnd(a, q.den, b, AV_ROUND_DOWN);
  103. return ((x_up > q.num) - (x_down < q.num)) * av_cmp_q(q2, q1);
  104. }
  105. int av_find_nearest_q_idx(AVRational q, const AVRational* q_list)
  106. {
  107. int i, nearest_q_idx = 0;
  108. for(i=0; q_list[i].den; i++)
  109. if (av_nearer_q(q, q_list[i], q_list[nearest_q_idx]) > 0)
  110. nearest_q_idx = i;
  111. return nearest_q_idx;
  112. }
  113. #ifdef TEST
  114. int main(void)
  115. {
  116. AVRational a,b;
  117. for(a.num=-2; a.num<=2; a.num++){
  118. for(a.den=-2; a.den<=2; a.den++){
  119. for(b.num=-2; b.num<=2; b.num++){
  120. for(b.den=-2; b.den<=2; b.den++){
  121. int c= av_cmp_q(a,b);
  122. double d= av_q2d(a) == av_q2d(b) ? 0 : (av_q2d(a) - av_q2d(b));
  123. if(d>0) d=1;
  124. else if(d<0) d=-1;
  125. else if(d != d) d= INT_MIN;
  126. if(c!=d) av_log(0, AV_LOG_ERROR, "%d/%d %d/%d, %d %f\n", a.num, a.den, b.num, b.den, c,d);
  127. }
  128. }
  129. }
  130. }
  131. return 0;
  132. }
  133. #endif