Revision 674bd4f6 libavutil/rational.c
libavutil/rational.c  

33  33 
#include "mathematics.h" 
34  34 
#include "rational.h" 
35  35  
36 
int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){


36 
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max){


37  37 
AVRational a0={0,1}, a1={1,0}; 
38 
int sign= (nom<0) ^ (den<0);


39 
int64_t gcd= av_gcd(FFABS(nom), FFABS(den));


38 
int sign= (num<0) ^ (den<0);


39 
int64_t gcd= av_gcd(FFABS(num), FFABS(den));


40  40  
41  41 
if(gcd){ 
42 
nom = FFABS(nom)/gcd;


42 
num = FFABS(num)/gcd;


43  43 
den = FFABS(den)/gcd; 
44  44 
} 
45 
if(nom<=max && den<=max){


46 
a1= (AVRational){nom, den};


45 
if(num<=max && den<=max){


46 
a1= (AVRational){num, den};


47  47 
den=0; 
48  48 
} 
49  49  
50  50 
while(den){ 
51 
uint64_t x = nom / den;


52 
int64_t next_den= nom  den*x;


51 
uint64_t x = num / den;


52 
int64_t next_den= num  den*x;


53  53 
int64_t a2n= x*a1.num + a0.num; 
54  54 
int64_t a2d= x*a1.den + a0.den; 
55  55  
...  ...  
57  57 
if(a1.num) x= (max  a0.num) / a1.num; 
58  58 
if(a1.den) x= FFMIN(x, (max  a0.den) / a1.den); 
59  59  
60 
if (den*(2*x*a1.den + a0.den) > nom*a1.den)


60 
if (den*(2*x*a1.den + a0.den) > num*a1.den)


61  61 
a1 = (AVRational){x*a1.num + a0.num, x*a1.den + a0.den}; 
62  62 
break; 
63  63 
} 
64  64  
65  65 
a0= a1; 
66  66 
a1= (AVRational){a2n, a2d}; 
67 
nom= den;


67 
num= den;


68  68 
den= next_den; 
69  69 
} 
70  70 
assert(av_gcd(a1.num, a1.den) <= 1U); 
71  71  
72 
*dst_nom = sign ? a1.num : a1.num;


72 
*dst_num = sign ? a1.num : a1.num;


73  73 
*dst_den = a1.den; 
74  74  
75  75 
return den==0; 
Also available in: Unified diff