Revision c11c2bc2 libavcodec/utils.c

View differences:

libavcodec/utils.c
31 31
#include <stdarg.h>
32 32
#include <limits.h>
33 33

  
34
const uint8_t ff_sqrt_tab[128]={
35
        0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
36
        5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
37
        8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
38
        9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11
39
};
40

  
41
const uint8_t ff_log2_tab[256]={
42
        0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
43
        5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
44
        6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
45
        6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
46
        7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
47
        7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
48
        7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
49
        7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
50
};
51

  
52 34
const uint8_t ff_reverse[256]={
53 35
0x00,0x80,0x40,0xC0,0x20,0xA0,0x60,0xE0,0x10,0x90,0x50,0xD0,0x30,0xB0,0x70,0xF0,
54 36
0x08,0x88,0x48,0xC8,0x28,0xA8,0x68,0xE8,0x18,0x98,0x58,0xD8,0x38,0xB8,0x78,0xF8,
......
938 920
    }
939 921
}
940 922

  
941
int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
942
    AVRational a0={0,1}, a1={1,0};
943
    int sign= (nom<0) ^ (den<0);
944
    int64_t gcd= ff_gcd(ABS(nom), ABS(den));
945

  
946
    nom = ABS(nom)/gcd;
947
    den = ABS(den)/gcd;
948
    if(nom<=max && den<=max){
949
        a1= (AVRational){nom, den};
950
        den=0;
951
    }
952
    
953
    while(den){
954
        int64_t x       = nom / den;
955
        int64_t next_den= nom - den*x;
956
        int64_t a2n= x*a1.num + a0.num;
957
        int64_t a2d= x*a1.den + a0.den;
958

  
959
        if(a2n > max || a2d > max) break;
960

  
961
        a0= a1;
962
        a1= (AVRational){a2n, a2d};
963
        nom= den;
964
        den= next_den;
965
    }
966
    assert(ff_gcd(a1.num, a1.den) == 1);
967
    
968
    *dst_nom = sign ? -a1.num : a1.num;
969
    *dst_den = a1.den;
970
    
971
    return den==0;
972
}
973

  
974
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd){
975
    AVInteger ai;
976
    int64_t r=0;
977
    assert(c > 0);
978
    assert(b >=0);
979
    assert(rnd >=0 && rnd<=5 && rnd!=4);
980
    
981
    if(a<0 && a != INT64_MIN) return -av_rescale_rnd(-a, b, c, rnd ^ ((rnd>>1)&1)); 
982
    
983
    if(rnd==AV_ROUND_NEAR_INF) r= c/2;
984
    else if(rnd&1)             r= c-1;
985

  
986
    if(b<=INT_MAX && c<=INT_MAX){
987
        if(a<=INT_MAX)
988
            return (a * b + r)/c;
989
        else
990
            return a/c*b + (a%c*b + r)/c;
991
    }
992
    
993
    ai= av_mul_i(av_int2i(a), av_int2i(b));
994
    ai= av_add_i(ai, av_int2i(r));
995
    
996
    return av_i2int(av_div_i(ai, av_int2i(c)));
997
}
998

  
999
int64_t av_rescale(int64_t a, int64_t b, int64_t c){
1000
    return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
1001
}
1002

  
1003
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq){
1004
    int64_t b= bq.num * (int64_t)cq.den;
1005
    int64_t c= cq.num * (int64_t)bq.den;
1006
    return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
1007
}
1008

  
1009
int64_t ff_gcd(int64_t a, int64_t b){
1010
    if(b) return ff_gcd(b, a%b);
1011
    else  return a;
1012
}
1013

  
1014
double av_int2dbl(int64_t v){
1015
    if(v+v > 0xFFELLU<<52)
1016
        return 0.0/0.0;
1017
    return ldexp(((v&(1LL<<52)-1) + (1LL<<52)) * (v>>63|1), (v>>52&0x7FF)-1075);
1018
}
1019

  
1020
float av_int2flt(int32_t v){
1021
    if(v+v > 0xFF000000U)
1022
        return 0.0/0.0;
1023
    return ldexp(((v&0x7FFFFF) + (1<<23)) * (v>>31|1), (v>>23&0xFF)-150);
1024
}
1025

  
1026
int64_t av_dbl2int(double d){
1027
    int e;
1028
    if     ( !d) return 0;
1029
    else if(d-d) return 0x7FF0000000000000LL + ((int64_t)(d<0)<<63) + (d!=d);
1030
    d= frexp(d, &e);
1031
    return (int64_t)(d<0)<<63 | (e+1022LL)<<52 | (int64_t)((fabs(d)-0.5)*(1LL<<53));
1032
}
1033

  
1034
int32_t av_flt2int(float d){
1035
    int e;
1036
    if     ( !d) return 0;
1037
    else if(d-d) return 0x7F800000 + ((d<0)<<31) + (d!=d);
1038
    d= frexp(d, &e);
1039
    return (d<0)<<31 | (e+126)<<23 | (int64_t)((fabs(d)-0.5)*(1<<24));
1040
}
1041

  
1042 923
/* av_log API */
1043 924

  
1044 925
static int av_log_level = AV_LOG_INFO;

Also available in: Unified diff