Libav 0.7.1
libavutil/rational.c
Go to the documentation of this file.
00001 /*
00002  * rational numbers
00003  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00028 #include "avassert.h"
00029 //#include <math.h>
00030 #include <limits.h>
00031 
00032 #include "common.h"
00033 #include "mathematics.h"
00034 #include "rational.h"
00035 
00036 int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max){
00037     AVRational a0={0,1}, a1={1,0};
00038     int sign= (num<0) ^ (den<0);
00039     int64_t gcd= av_gcd(FFABS(num), FFABS(den));
00040 
00041     if(gcd){
00042         num = FFABS(num)/gcd;
00043         den = FFABS(den)/gcd;
00044     }
00045     if(num<=max && den<=max){
00046         a1= (AVRational){num, den};
00047         den=0;
00048     }
00049 
00050     while(den){
00051         uint64_t x      = num / den;
00052         int64_t next_den= num - den*x;
00053         int64_t a2n= x*a1.num + a0.num;
00054         int64_t a2d= x*a1.den + a0.den;
00055 
00056         if(a2n > max || a2d > max){
00057             if(a1.num) x= (max - a0.num) / a1.num;
00058             if(a1.den) x= FFMIN(x, (max - a0.den) / a1.den);
00059 
00060             if (den*(2*x*a1.den + a0.den) > num*a1.den)
00061                 a1 = (AVRational){x*a1.num + a0.num, x*a1.den + a0.den};
00062             break;
00063         }
00064 
00065         a0= a1;
00066         a1= (AVRational){a2n, a2d};
00067         num= den;
00068         den= next_den;
00069     }
00070     av_assert2(av_gcd(a1.num, a1.den) <= 1U);
00071 
00072     *dst_num = sign ? -a1.num : a1.num;
00073     *dst_den = a1.den;
00074 
00075     return den==0;
00076 }
00077 
00078 AVRational av_mul_q(AVRational b, AVRational c){
00079     av_reduce(&b.num, &b.den, b.num * (int64_t)c.num, b.den * (int64_t)c.den, INT_MAX);
00080     return b;
00081 }
00082 
00083 AVRational av_div_q(AVRational b, AVRational c){
00084     return av_mul_q(b, (AVRational){c.den, c.num});
00085 }
00086 
00087 AVRational av_add_q(AVRational b, AVRational c){
00088     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);
00089     return b;
00090 }
00091 
00092 AVRational av_sub_q(AVRational b, AVRational c){
00093     return av_add_q(b, (AVRational){-c.num, c.den});
00094 }
00095 
00096 AVRational av_d2q(double d, int max){
00097     AVRational a;
00098 #define LOG2  0.69314718055994530941723212145817656807550013436025
00099     int exponent;
00100     int64_t den;
00101     if (isnan(d))
00102         return (AVRational){0,0};
00103     if (isinf(d))
00104         return (AVRational){ d<0 ? -1:1, 0 };
00105     exponent = FFMAX( (int)(log(fabs(d) + 1e-20)/LOG2), 0);
00106     den = 1LL << (61 - exponent);
00107     av_reduce(&a.num, &a.den, (int64_t)(d * den + 0.5), den, max);
00108 
00109     return a;
00110 }
00111 
00112 int av_nearer_q(AVRational q, AVRational q1, AVRational q2)
00113 {
00114     /* n/d is q, a/b is the median between q1 and q2 */
00115     int64_t a = q1.num * (int64_t)q2.den + q2.num * (int64_t)q1.den;
00116     int64_t b = 2 * (int64_t)q1.den * q2.den;
00117 
00118     /* rnd_up(a*d/b) > n => a*d/b > n */
00119     int64_t x_up = av_rescale_rnd(a, q.den, b, AV_ROUND_UP);
00120 
00121     /* rnd_down(a*d/b) < n => a*d/b < n */
00122     int64_t x_down = av_rescale_rnd(a, q.den, b, AV_ROUND_DOWN);
00123 
00124     return ((x_up > q.num) - (x_down < q.num)) * av_cmp_q(q2, q1);
00125 }
00126 
00127 int av_find_nearest_q_idx(AVRational q, const AVRational* q_list)
00128 {
00129     int i, nearest_q_idx = 0;
00130     for(i=0; q_list[i].den; i++)
00131         if (av_nearer_q(q, q_list[i], q_list[nearest_q_idx]) > 0)
00132             nearest_q_idx = i;
00133 
00134     return nearest_q_idx;
00135 }
00136 
00137 #ifdef TEST
00138 main(){
00139     AVRational a,b;
00140     for(a.num=-2; a.num<=2; a.num++){
00141         for(a.den=-2; a.den<=2; a.den++){
00142             for(b.num=-2; b.num<=2; b.num++){
00143                 for(b.den=-2; b.den<=2; b.den++){
00144                     int c= av_cmp_q(a,b);
00145                     double d= av_q2d(a) == av_q2d(b) ? 0 : (av_q2d(a) - av_q2d(b));
00146                     if(d>0) d=1;
00147                     else if(d<0) d=-1;
00148                     else if(d != d) d= INT_MIN;
00149                     if(c!=d) av_log(0, AV_LOG_ERROR, "%d/%d %d/%d, %d %f\n", a.num, a.den, b.num, b.den, c,d);
00150                 }
00151             }
00152         }
00153     }
00154 }
00155 #endif