~ubuntu-branches/ubuntu/hardy/avidemux/hardy

« back to all changes in this revision

Viewing changes to avidemux/ADM_lavutil/rational.c

  • Committer: Bazaar Package Importer
  • Author(s): Matvey Kozhev
  • Date: 2007-12-18 13:53:04 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20071218135304-cdqec2lg2bglyz15
Tags: 1:2.4~preview3-0.0ubuntu1
* Upload to Ubuntu. (LP: #163287, LP: #126572)
* debian/changelog: re-added Ubuntu releases.
* debian/control:
  - Require debhelper >= 5.0.51 (for dh_icons) and imagemagick.
  - Build-depend on libsdl1.2-dev instead of libsdl-dev.
  - Build against newer libx264-dev. (LP: #138854)
  - Removed libamrnb-dev, not in Ubuntu yet.
* debian/rules:
  - Install all icon sizes, using convert (upstream installs none).
  - Added missing calls to dh_installmenu, dh_installman, dh_icons and
    dh_desktop.
* debian/menu, debian/avidemux-qt.menu:
  - Corrected package and executable names.
* debian/avidemux-common.install: Install icons.
* debian/avidemux.common.manpages: Install man/avidemux.1.
* debian/links, debian/avidemux-cli.links, debian/avidemux-gtk.links:
  - Link manpages to avidemux.1.gz.
* debian/install, debian/avidemux-qt.install, debian/avidemux-gtk.desktop,
  debian/avidemux-qt.desktop: Install desktop files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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., 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 <michaelni@gmx.at>
25
 
 */
26
 
 
27
 
//#include <math.h>
28
 
#include <limits.h>
29
 
 
30
 
#include "common.h"
31
 
#include "mathematics.h"
32
 
#include "rational.h"
33
 
 
34
 
int 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
 
 */
70
 
AVRational 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
 
 */
78
 
AVRational av_div_q(AVRational b, AVRational c){
79
 
    return av_mul_q(b, (AVRational){c.den, c.num});
80
 
}
81
 
 
82
 
/**
83
 
 * returns b+c.
84
 
 */
85
 
AVRational av_add_q(AVRational b, AVRational c){
86
 
    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);
87
 
    return b;
88
 
}
89
 
 
90
 
/**
91
 
 * returns b-c.
92
 
 */
93
 
AVRational av_sub_q(AVRational b, AVRational c){
94
 
    return av_add_q(b, (AVRational){-c.num, c.den});
95
 
}
96
 
 
97
 
/**
98
 
 * Converts a double precission floating point number to a AVRational.
99
 
 * @param max the maximum allowed numerator and denominator
100
 
 */
101
 
AVRational av_d2q(double d, int max){
102
 
    AVRational a;
103
 
#define LOG2  0.69314718055994530941723212145817656807550013436025
104
 
    int exponent= FFMAX( (int)(log(fabs(d) + 1e-20)/LOG2), 0);
105
 
    int64_t den= 1LL << (61 - exponent);
106
 
    av_reduce(&a.num, &a.den, (int64_t)(d * den + 0.5), den, max);
107
 
 
108
 
    return a;
109
 
}