annotate rational.c @ 2490:c2c642ad6ef4 libavcodec

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