rational.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 libavutil/rational.c
  23. * rational numbers
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include <assert.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. assert(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= FFMAX( (int)(log(fabs(d) + 1e-20)/LOG2), 0);
  84. int64_t den= 1LL << (61 - exponent);
  85. av_reduce(&a.num, &a.den, (int64_t)(d * den + 0.5), den, max);
  86. return a;
  87. }
  88. int av_nearer_q(AVRational q, AVRational q1, AVRational q2)
  89. {
  90. /* n/d is q, a/b is the median between q1 and q2 */
  91. int64_t a = q1.num * (int64_t)q2.den + q2.num * (int64_t)q1.den;
  92. int64_t b = 2 * (int64_t)q1.den * q2.den;
  93. /* rnd_up(a*d/b) > n => a*d/b > n */
  94. int64_t x_up = av_rescale_rnd(a, q.den, b, AV_ROUND_UP);
  95. /* rnd_down(a*d/b) < n => a*d/b < n */
  96. int64_t x_down = av_rescale_rnd(a, q.den, b, AV_ROUND_DOWN);
  97. return ((x_up > q.num) - (x_down < q.num)) * av_cmp_q(q2, q1);
  98. }
  99. int av_find_nearest_q_idx(AVRational q, const AVRational* q_list)
  100. {
  101. int i, nearest_q_idx = 0;
  102. for(i=0; q_list[i].den; i++)
  103. if (av_nearer_q(q, q_list[i], q_list[nearest_q_idx]) > 0)
  104. nearest_q_idx = i;
  105. return nearest_q_idx;
  106. }