Statistics
| Branch: | Revision:

ffmpeg / libavutil / mathematics.c @ c11c2bc2

History | View | Annotate | Download (3.01 KB)

1
/*
2
 * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
3
 *
4
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2 of the License, or (at your option) any later version.
8
 *
9
 * This library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with this library; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 */
18
 
19
/**
20
 * @file mathematics.c
21
 * Miscellaneous math routines and tables.
22
 */
23

    
24
#include "common.h"
25
#include "integer.h"
26
#include "mathematics.h"
27

    
28
const uint8_t ff_sqrt_tab[128]={
29
        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,
30
        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,
31
        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,
32
        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
33
};
34

    
35
const uint8_t ff_log2_tab[256]={
36
        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,
37
        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,
38
        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,
39
        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,
40
        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,
41
        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,
42
        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,
43
        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
44
};
45

    
46
int64_t ff_gcd(int64_t a, int64_t b){
47
    if(b) return ff_gcd(b, a%b);
48
    else  return a;
49
}
50

    
51
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd){
52
    AVInteger ai;
53
    int64_t r=0;
54
    assert(c > 0);
55
    assert(b >=0);
56
    assert(rnd >=0 && rnd<=5 && rnd!=4);
57
    
58
    if(a<0 && a != INT64_MIN) return -av_rescale_rnd(-a, b, c, rnd ^ ((rnd>>1)&1)); 
59
    
60
    if(rnd==AV_ROUND_NEAR_INF) r= c/2;
61
    else if(rnd&1)             r= c-1;
62

    
63
    if(b<=INT_MAX && c<=INT_MAX){
64
        if(a<=INT_MAX)
65
            return (a * b + r)/c;
66
        else
67
            return a/c*b + (a%c*b + r)/c;
68
    }
69
    
70
    ai= av_mul_i(av_int2i(a), av_int2i(b));
71
    ai= av_add_i(ai, av_int2i(r));
72
    
73
    return av_i2int(av_div_i(ai, av_int2i(c)));
74
}
75

    
76
int64_t av_rescale(int64_t a, int64_t b, int64_t c){
77
    return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
78
}
79

    
80
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq){
81
    int64_t b= bq.num * (int64_t)cq.den;
82
    int64_t c= cq.num * (int64_t)bq.den;
83
    return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
84
}