Revision 5f63d108

View differences:

libavcodec/utils.c
786 786
}
787 787

  
788 788
int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
789
    int exact=1, sign=0;
790
    int64_t gcd;
791

  
792
    assert(den != 0);
793

  
794
    if(den < 0)
795
        return av_reduce(dst_nom, dst_den, -nom, -den, max);
796
    
797
    sign= nom < 0;
798
    nom= ABS(nom);
799
    
800
    gcd = ff_gcd(nom, den);
801
    nom /= gcd;
802
    den /= gcd;
803
    
804
    if(nom > max || den > max){
805
        AVRational a0={0,1}, a1={1,0};
806
        exact=0;
807

  
808
        for(;;){
809
            int64_t x= nom / den;
810
            int64_t a2n= x*a1.num + a0.num;
811
            int64_t a2d= x*a1.den + a0.den;
812

  
813
            if(a2n > max || a2d > max) break;
814

  
815
            nom %= den;
816
        
817
            a0= a1;
818
            a1= (AVRational){a2n, a2d};
819
            if(nom==0) break;
820
            x= nom; nom=den; den=x;
821
        }
822
        nom= a1.num;
823
        den= a1.den;
789
    AVRational a0={0,1}, a1={1,0};
790
    int sign= (nom<0) ^ (den<0);
791
    int64_t gcd= ff_gcd(ABS(nom), ABS(den));
792

  
793
    nom = ABS(nom)/gcd;
794
    den = ABS(den)/gcd;
795
    if(nom<=max && den<=max){
796
        a1= (AVRational){nom, den};
797
        den=0;
824 798
    }
825 799
    
826
    assert(ff_gcd(nom, den) == 1);
800
    while(den){
801
        int64_t x       = nom / den;
802
        int64_t next_den= nom - den*x;
803
        int64_t a2n= x*a1.num + a0.num;
804
        int64_t a2d= x*a1.den + a0.den;
805

  
806
        if(a2n > max || a2d > max) break;
807

  
808
        a0= a1;
809
        a1= (AVRational){a2n, a2d};
810
        nom= den;
811
        den= next_den;
812
    }
813
    assert(ff_gcd(a1.num, a1.den) == 1);
827 814
    
828
    *dst_nom = sign ? -nom : nom;
829
    *dst_den = den;
815
    *dst_nom = sign ? -a1.num : a1.num;
816
    *dst_den = a1.den;
830 817
    
831
    return exact;
818
    return den==0;
832 819
}
833 820

  
834 821
int64_t av_rescale(int64_t a, int64_t b, int64_t c){

Also available in: Unified diff