VirtualBox

source: vbox/trunk/src/libs/ffmpeg-20060710/libavutil/rational.c@ 11532

Last change on this file since 11532 was 5776, checked in by vboxsync, 17 years ago

ffmpeg: exported to OSE

File size: 2.9 KB
Line 
1/*
2 * Rational numbers
3 * Copyright (c) 2003 Michael Niedermayer <[email protected]>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21/**
22 * @file rational.c
23 * Rational numbers
24 * @author Michael Niedermayer <[email protected]>
25 */
26
27//#include <math.h>
28#include <limits.h>
29
30#include "common.h"
31#include "mathematics.h"
32#include "rational.h"
33
34int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
35 AVRational a0={0,1}, a1={1,0};
36 int sign= (nom<0) ^ (den<0);
37 int64_t gcd= ff_gcd(ABS(nom), ABS(den));
38
39 nom = ABS(nom)/gcd;
40 den = ABS(den)/gcd;
41 if(nom<=max && den<=max){
42 a1= (AVRational){nom, den};
43 den=0;
44 }
45
46 while(den){
47 int64_t x = nom / den;
48 int64_t next_den= nom - den*x;
49 int64_t a2n= x*a1.num + a0.num;
50 int64_t a2d= x*a1.den + a0.den;
51
52 if(a2n > max || a2d > max) break;
53
54 a0= a1;
55 a1= (AVRational){a2n, a2d};
56 nom= den;
57 den= next_den;
58 }
59 assert(ff_gcd(a1.num, a1.den) == 1);
60
61 *dst_nom = sign ? -a1.num : a1.num;
62 *dst_den = a1.den;
63
64 return den==0;
65}
66
67/**
68 * returns b*c.
69 */
70AVRational av_mul_q(AVRational b, AVRational c){
71 av_reduce(&b.num, &b.den, b.num * (int64_t)c.num, b.den * (int64_t)c.den, INT_MAX);
72 return b;
73}
74
75/**
76 * returns b/c.
77 */
78AVRational av_div_q(AVRational b, AVRational c){
79 av_reduce(&b.num, &b.den, b.num * (int64_t)c.den, b.den * (int64_t)c.num, INT_MAX);
80 return b;
81}
82
83/**
84 * returns b+c.
85 */
86AVRational av_add_q(AVRational b, AVRational c){
87 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);
88 return b;
89}
90
91/**
92 * returns b-c.
93 */
94AVRational av_sub_q(AVRational b, AVRational c){
95 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);
96 return b;
97}
98
99/**
100 * Converts a double precission floating point number to a AVRational.
101 * @param max the maximum allowed numerator and denominator
102 */
103AVRational av_d2q(double d, int max){
104 AVRational a;
105 int exponent= FFMAX( (int)(log(ABS(d) + 1e-20)/log(2)), 0);
106 int64_t den= 1LL << (61 - exponent);
107 av_reduce(&a.num, &a.den, (int64_t)(d * den + 0.5), den, max);
108
109 return a;
110}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette