Libav
|
00001 /* 00002 * rational numbers 00003 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at> 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00028 #include <assert.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 assert(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= FFMAX( (int)(log(fabs(d) + 1e-20)/LOG2), 0); 00100 int64_t den= 1LL << (61 - exponent); 00101 if (isnan(d)) 00102 return (AVRational){0,0}; 00103 av_reduce(&a.num, &a.den, (int64_t)(d * den + 0.5), den, max); 00104 00105 return a; 00106 } 00107 00108 int av_nearer_q(AVRational q, AVRational q1, AVRational q2) 00109 { 00110 /* n/d is q, a/b is the median between q1 and q2 */ 00111 int64_t a = q1.num * (int64_t)q2.den + q2.num * (int64_t)q1.den; 00112 int64_t b = 2 * (int64_t)q1.den * q2.den; 00113 00114 /* rnd_up(a*d/b) > n => a*d/b > n */ 00115 int64_t x_up = av_rescale_rnd(a, q.den, b, AV_ROUND_UP); 00116 00117 /* rnd_down(a*d/b) < n => a*d/b < n */ 00118 int64_t x_down = av_rescale_rnd(a, q.den, b, AV_ROUND_DOWN); 00119 00120 return ((x_up > q.num) - (x_down < q.num)) * av_cmp_q(q2, q1); 00121 } 00122 00123 int av_find_nearest_q_idx(AVRational q, const AVRational* q_list) 00124 { 00125 int i, nearest_q_idx = 0; 00126 for(i=0; q_list[i].den; i++) 00127 if (av_nearer_q(q, q_list[i], q_list[nearest_q_idx]) > 0) 00128 nearest_q_idx = i; 00129 00130 return nearest_q_idx; 00131 }