1548
|
1 /*
|
|
2 * Rational numbers
|
|
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
|
|
4 *
|
|
5 * This library is free software; you can redistribute it and/or
|
|
6 * modify it under the terms of the GNU Lesser General Public
|
|
7 * License as published by the Free Software Foundation; either
|
|
8 * version 2 of the License, or (at your option) any later version.
|
|
9 *
|
|
10 * This library is distributed in the hope that it will be useful,
|
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13 * Lesser General Public License for more details.
|
|
14 *
|
|
15 * You should have received a copy of the GNU Lesser General Public
|
|
16 * License along with this library; if not, write to the Free Software
|
|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18 *
|
|
19 */
|
|
20
|
|
21 /**
|
|
22 * @file rational.c
|
|
23 * Rational numbers
|
|
24 * @author Michael Niedermayer <michaelni@gmx.at>
|
|
25 */
|
|
26
|
|
27 //#include <math.h>
|
|
28 #include <limits.h>
|
|
29
|
|
30 #include "common.h"
|
|
31 #include "avcodec.h"
|
|
32 #include "rational.h"
|
|
33
|
2127
|
34 /**
|
|
35 * returns b*c.
|
|
36 */
|
1548
|
37 AVRational av_mul_q(AVRational b, AVRational c){
|
|
38 av_reduce(&b.num, &b.den, b.num * (int64_t)c.num, b.den * (int64_t)c.den, INT_MAX);
|
|
39 return b;
|
|
40 }
|
|
41
|
2127
|
42 /**
|
|
43 * returns b/c.
|
|
44 */
|
1548
|
45 AVRational av_div_q(AVRational b, AVRational c){
|
|
46 av_reduce(&b.num, &b.den, b.num * (int64_t)c.den, b.den * (int64_t)c.num, INT_MAX);
|
|
47 return b;
|
|
48 }
|
|
49
|
2127
|
50 /**
|
|
51 * returns b+c.
|
|
52 */
|
1548
|
53 AVRational av_add_q(AVRational b, AVRational c){
|
|
54 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);
|
|
55 return b;
|
|
56 }
|
|
57
|
2127
|
58 /**
|
|
59 * returns b-c.
|
|
60 */
|
1548
|
61 AVRational av_sub_q(AVRational b, AVRational c){
|
|
62 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);
|
|
63 return b;
|
|
64 }
|
|
65
|
1625
|
66 /**
|
|
67 * Converts a double precission floating point number to a AVRational.
|
|
68 * @param max the maximum allowed numerator and denominator
|
|
69 */
|
1548
|
70 AVRational av_d2q(double d, int max){
|
|
71 AVRational a;
|
1551
|
72 int exponent= FFMAX( (int)(log(ABS(d) + 1e-20)/log(2)), 0);
|
1548
|
73 int64_t den= 1LL << (61 - exponent);
|
|
74 av_reduce(&a.num, &a.den, (int64_t)(d * den + 0.5), den, max);
|
|
75
|
|
76 return a;
|
|
77 }
|